package com.slipper.system.schedule.controller;

import com.google.gson.reflect.TypeToken;
import com.slipper.core.gson.GsonUtil;
import com.slipper.system.schedule.bo.ScheduleBO;
import com.slipper.util.AESUtil;
import com.slipper.util.DateUtil;
import com.slipper.util.FileUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.slipper.system.schedule.service.ScheduleService;
import com.slipper.util.ResultUtil;

import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;

/**
 * 任务调度
 */
@RestController
@RequestMapping("/system/schedule")
public class ScheduleController {
    private static final Logger logger = LoggerFactory.getLogger(ScheduleController.class);

    @Resource
    private ScheduleService scheduleService;
    
    @PostMapping("/queryBy")
    public String queryBy(@RequestBody ScheduleBO bo) {
        return ResultUtil.successResult(scheduleService.queryBy(bo.getId()));
    }
    
    @PostMapping("/queryPage")
    public String queryPage(@RequestBody ScheduleBO bo) {
        return ResultUtil.successResult(scheduleService.queryPage(bo));
    }

    @PostMapping("/save")
    public String save(@RequestBody ScheduleBO bo) {
        return ResultUtil.saveResult(scheduleService.save(bo));
    }

    @PostMapping("/delete")
    public String delete(@RequestBody ScheduleBO bo) {
        return ResultUtil.deleteResult(scheduleService.delete(bo));
    }
    
    /**
     * 暂停任务
     */
    @RequestMapping("/pause")
    public String pause(@RequestBody ScheduleBO bo) {
        bo.setStatus(ScheduleBO.STATUS_PAUSE);
        return ResultUtil.operateResult(this.scheduleService.updateStatus(bo), "暂停");
    }
    
    /**
     * 恢复任务
     */
    @RequestMapping("/resume")
    public String resume(@RequestBody ScheduleBO bo) {
        bo.setStatus(ScheduleBO.STATUS_NORMAL);
        return ResultUtil.operateResult(this.scheduleService.updateStatus(bo), "恢复");
    }
    
    /**
     * 立即执行任务
     */
    @RequestMapping("/runTask")
    public String runTask(@RequestBody ScheduleBO bo) {
        return ResultUtil.operateResult(this.scheduleService.runTask(bo), "执行任务");
    }


    @PostMapping(value = "/exportData")
    public void exportData(HttpServletResponse response, @RequestBody ScheduleBO bo) throws IOException {
        if (bo.getIds() == null || bo.getIds().length == 0) {
            ResultUtil.failureResult(response, 400, "参数不正确！");
            return;
        }

        ScheduleBO scheduleQO = new ScheduleBO();
        scheduleQO.setIds(bo.getIds());
        List<ScheduleBO> scheduleList = scheduleService.queryList(scheduleQO);
        String fileName = "schedule-" + DateUtil.format(new Date(), "dd-HH-mm-ss") + ".dat";
        FileUtil.downloadContent(response, fileName, AESUtil.encrypt(GsonUtil.toJson(scheduleList)));
    }

    @PostMapping(value = "/importData")
    public String importData(ScheduleBO bo) {
        if (bo.getFile() == null) {
            return ResultUtil.failureResult("请选择上传文件！");
        }

        try {
            String content = new String(bo.getFile().getBytes(), StandardCharsets.UTF_8);
            Type type = new TypeToken<List<ScheduleBO>>(){}.getType();
            List<ScheduleBO> scheduleList = GsonUtil.fromJson(AESUtil.decrypt(content), type);
            scheduleService.saveList(scheduleList);
            return ResultUtil.successResult("导入调度成功。");
        } catch (Exception e) {
            logger.error("文件解释出错！", e);
            return ResultUtil.failureResult("文件解释出错！");
        }
    }
}
